package cn.itsource.pethome.pet.controller;

import cn.itsource.pethome.loginutil.LoginContextUtils;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.pet.domain.Pet;
import cn.itsource.pethome.pet.query.PetQuery;
import cn.itsource.pethome.pet.service.IPetService;
import cn.itsource.pethome.util.AjaxResult;
import cn.itsource.pethome.util.BusinessException;
import cn.itsource.pethome.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;
    @Autowired
    private LoginContextUtils loginContextUtils;


    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet, HttpServletRequest request){
        Employee loginUser = (Employee)loginContextUtils.getLoginUser(request);
        try {
            if( pet.getId()!=null)
                petService.update(pet);
            else
                petService.insert(pet,loginUser);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id")Long id)
    {
        return petService.queryOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Pet> list(){

        return petService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Pet> json(@RequestBody PetQuery query)
    {
        return petService.pageQueryAll(query);
    }

    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        //todo 当前人设置进去
        try {
            petService.offsale(ids);
            return AjaxResult.me();
        }catch (BusinessException e){//你的业务出现问题了
            return AjaxResult.error().setMsg(e.getMessage());
        }catch (Exception e) {//系统级别的错误
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        //todo 当前人设置进去
        try {
            Map<String,Object> map = petService.onsale(ids);
            return AjaxResult.me().setData(map);
        }catch (BusinessException e){//你的业务出现问题了
            return AjaxResult.error().setMsg(e.getMessage());
        }catch (Exception e) {//系统级别的错误
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
